﻿using Microsoft.Practices.Prism.Commands;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Windows.Input;

namespace HobSoft.SoccerBet2014.Client.ViewModels
{
    using Configuration;
    public class ConfigurationShellModel : CommandViewModel
    {
        public ICommand LoadKeySettingCommand { get; protected set; }
        public ICommand SaveKeySettingCommand { get; protected set; }

        private readonly BettingKeyRepository keyRepository;
        public ConfigurationShellModel(BettingKeyRepository _keyRepository)
        {
            keyRepository = _keyRepository;
            BettingKeyConfiguration = new ObservableCollection<BettingKeySetting>();
            LoadKeySettingCommand = new DelegateCommand(LoadKeySetting);
            SaveKeySettingCommand = new DelegateCommand(SaveKeySetting);

            InitializeModels();
        }
        private bool _onConfigInitialize = false;
        protected void InitializeModels()
        {
            _onConfigInitialize = true;
            BettingKeyConfiguration.CollectionChanged += BettingKeyConfiguration_CollectionChanged;
            keyRepository.RegisterDefault();
            keyRepository.Submit();

            var keys = keyRepository.GetMany(b => true);
            BettingKeyConfiguration.Clear();
            var query = keys.OrderBy(k => k.Name);
            foreach (var key in query)
            {
                BettingKeyConfiguration.Add(key);
            }
            CreateKeyBinding();
            _onConfigInitialize = false;
        }

        protected void CreateKeyBinding()
        {
            System.Collections.Generic.List<KeyBinding> keybindings = new System.Collections.Generic.List<KeyBinding>();
            var query = BettingKeyConfiguration.Where(k => k.HotKeyCommand == HotKeyCommand.Betting);
            foreach (var keySetting in query)
            {
                var keybinding = new KeyBinding();
                keybinding.Key = keySetting.Key;
                if (keySetting.Gesture != null)
                    keybinding.Gesture = keySetting.Gesture;

                keybinding.CommandParameter = keySetting.Money;
                keybindings.Add(keybinding);
            }
            BettingKeys = keybindings;
        }

        private void BettingKeyConfiguration_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                if (!_onConfigInitialize)
                    CreateKeyBinding();
            }
        }

        public void LoadKeySetting()
        {
            keyRepository.Initialize();
            var keys = keyRepository.GetMany(b => true);
            BettingKeyConfiguration.Clear();
            foreach (var key in keys)
            {
                BettingKeyConfiguration.Add(key);
            }
        }

        public void SaveKeySetting()
        {
            var existsKey = BettingKeyConfiguration.Select(b => b.Key).ToArray();
            keyRepository.DeleteMany(k => !existsKey.Contains(k.Key));
            keyRepository.AddOrUpdate(BettingKeyConfiguration);
            keyRepository.Submit();
            Properties.Settings.Default.Save();
        }

        protected BettingKeySetting GetBettingKeySetting(string name)
        {
            return BettingKeyConfiguration.SingleOrDefault(b => b.Name == name);
        }

        private ObservableCollection<BettingKeySetting> _bettingKeyConfiguration;

        public ObservableCollection<BettingKeySetting> BettingKeyConfiguration
        {
            get { return _bettingKeyConfiguration; }
            set
            {
                PropertyValueChange(ref _bettingKeyConfiguration, value, "BettingKeyConfiguration");
                if (_bettingKeyConfiguration != null && _bettingKeyConfiguration.Count > 0)
                    CreateKeyBinding();
            }
        }

        private IList<KeyBinding> _bettingKeys;
        public IList<KeyBinding> BettingKeys
        {
            get
            {
                return _bettingKeys;
            }
            protected set
            {
                PropertyValueChange(ref _bettingKeys, value, "BettingKeys");
            }
        }


    }
}
